using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using DomainModel.Entities;
using DomainModel.Abstract;

namespace WebSite.Controllers
{
    // The controller for dealing with views regarding the creation and
    // editing of job types and the ability to associate questions with them.
    [Authorize(Roles = "HiringManager")]
    public class StoresManagementController : Controller
    {
        // The database abstractions for the jobs, questions, questionnaireEntries, and positions.
        private IStoresRepository storesRepository;

        // Constructor, it attachs the jobs repository, the questions repository, 
        // the questionnaireEntry repository, and the positions repository to the 
        // controller.
        public StoresManagementController(IStoresRepository storesRepository)
        {
            this.storesRepository = storesRepository;

            TempData["error"] = "";
        }

        // This is just a wrapper for the query to get a job given an id.
        private Store GetStore(int storeID)
        {
            Store store = (from p in storesRepository.Stores
                       where p.StoreID == storeID
                       select p).First();
            return store;
        }

        // Provides list of jobs in repository as model to Index view.
        public ViewResult Index()
        {

            return View(storesRepository.Stores.ToList());
        }

        // Instantiates a new job and passes it to the Edit view.
        public ActionResult Create()
        {
            return View("Edit", new Store());
        }

        // Edit command for http get, returns a job given an id
        // and passes the job to the view.
        [AcceptVerbs(HttpVerbs.Get)]
        public ViewResult Edit(int storeID)
        {
            Store store = GetStore(storeID);

            return View(store);
        }

        // Edit command for post, places a job from the view into
        // the repository.
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(Store store)
        {
            if (ModelState.IsValid)
            {
                storesRepository.SaveStore(store);
                return RedirectToAction("Index");
            }
            else
            {
                return View(store);
            }
        }

        // The Http-Get for delete job. Provide job to be deleted
        // to the view.
        public ActionResult Delete(int storeID)
        {
            Store store = GetStore(storeID);

            return View(store);
        }

        // This deletes a job from the repository given a
        // id from the view. Also deletes any bridge entries between
        // the job and the questions. It will not delete anything if
        // there are any answeredQuestions pointing at the attached
        // questions or Positions pointing at the job.
        [AcceptVerbs(HttpVerbs.Post)]
        public RedirectToRouteResult Delete(int storeID, string confirmButton)
        {
            Store store = GetStore(storeID);

            storesRepository.DeleteStore(store);

            return RedirectToAction("Index");
        }

        //[ActionName("Select")]
        //[AcceptVerbs(HttpVerbs.Post)]
        //public ActionResult Select_GET(int storeID)
        //{
        //    Store store = GetStore(storeID);//ViewData["store"] = storeID;

        //    return View(store);
        //}

        [ActionName("Select")]
        [AcceptVerbs(HttpVerbs.Get)]
        public RedirectToRouteResult Select_POST(int storeID)
        {
            //ViewData["store"] = storeID;

            return RedirectToAction("Index", "PositionsManagement", new { storeID });
        }

        public ViewResult List()
        {
            return View(storesRepository.Stores.ToList());
        }
    }
}
